<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(0); 
class ewap extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('html','url'));
		$this->load->model('pub_model');
	}
	
	public function index()
	{
		//$this->output->enable_profiler(TRUE);
		$data['doctype'] = doctype('xhtml1-trans');
		$data['meta_charset'] = meta('Content-type','text/html;charset=utf-8','equiv');
		$data['link'] = link_tag('css/ewap.css');
		$data['jquery'] = '<script src="'.base_url().'script/jquery.js"></script>';
		
		$data['skey']=SinaAppKey;
		$data['tkey']=TencentAppKey;	
		
		$l='';	
		
		$re='';
		$po='s';
		
		if(!empty($_GET['po'])){
			$po=$_GET['po'];
		}
		
		if($po=='s'){
			if(!empty($_GET['code'])){
				$ad=$_GET['code'];
	
				$url='https://api.weibo.com/oauth2/access_token';
				$key=SinaAppKey;
				$secret=SinaAppSecret;
				$type='authorization_code';
				$code=$_GET['code'];
				$uri=base_url().'index.php?c=ewap&m=index';
	
				$re=$this->getTk($url,$key,$secret,$type,$code,$uri);
	
				if($re!=''){
					$obj=json_decode($re);
		
					$cookie = array(
					    'name'   => 'di',
					    'value'  => $obj->{'uid'}.'_'.$obj->{'access_token'}.'_s',
					    'expire' => $obj->{'expires_in'}
					);

					$this->input->set_cookie($cookie);
				}
				
				$l='s';
			}
		}else{
			if(!empty($_GET['code'])){
				$ad=$_GET['code'];
	
				$url='https://graph.qq.com/oauth2.0/token';
				$key=TencentAppKey;
				$secret=TencentAppSecret;
				$type='authorization_code';
				$code=$_GET['code'];
				$uri=base_url().'index.php?c=ewap&m=index';
	
				$re=$this->getTk($url,$key,$secret,$type,$code,$uri);
				
				$access_token='';
				$expires_in='';
				
				$arr=array();
	
				if($re!=''){
					$tmp=explode('&',$re);
					foreach($tmp as $t)
					{
						list($k,$v)=explode('=',$t);
						$arr[$k]=$v;
					}
					
					$access_token=$arr[access_token];
					$expires_in=(INT)$arr[expires_in];
				}
				
				$url='https://graph.z.qq.com/moc2/me';
				
				$re=$this->getQOID($url,$access_token);
				
				$uid='';
				
				if($re!=''){
					$tmp=explode('&',$re);
					foreach($tmp as $t)
					{
						list($k,$v)=explode('=',$t);
						$arr[$k]=$v;
					}
					
					$uid=$arr[openid];
				}
		
				$cookie = array(
				    'name'   => 'di',
				    'value'  => $uid.'_'.$access_token.'_t',
				    'expire' => $expires_in
				);

				$this->input->set_cookie($cookie);
			}
			
			$l='t';		
		}
		
		$data['l']=$l;
		$this->load->view('ewap/index',$data);
	}
	
	public function save(){
		$url='';
		$access_token=urldecode($_POST['access_token']);
		$openid=urldecode($_POST['openid']);
		$status=urldecode($_POST['status']);
		$content=urldecode($_POST['content']);
		$pic=urldecode($_POST['pic']);
		
		if($status=='tencent'){
			$url='https://graph.qq.com/t/add_t';
			$re=$this->setTWB($url,$access_token,'#怪物神枪手Luckyline#'.$content.'http://lucky.morningtec.cn',TencentAppKey,$openid);
		}else{
			$url='https://api.weibo.com/2/statuses/update.json';
			$re=$this->setWB($url,$access_token,'#怪物神枪手Luckyline#'.$content.'http://lucky.morningtec.cn');
		}
		
		echo $this->pub_model->save($openid,$status,$content,$pic);
	}
	
	public function countnum(){	
		$result=$this->pub_model->countnum();
		$str='';
		
		foreach($result->result() as $row){
			$str=$row->countnum;
		}
		
		echo $str;
	}
	
	function getQOID($url,$access_token){
		$pdata = array ('access_token' => $access_token);
		$pdata = http_build_query($pdata);

		$params = array('http' => array(
			'method' => 'POST',
			'content' => $pdata
		));

		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		$response = @stream_get_contents($fp);
		
		return $response;
	}
	
	function getTk($url,$key,$secret,$type,$code,$uri){
		$pdata = array ('client_id' => $key,
			'client_secret' => $secret,
			'grant_type' => $type,
			'code' => $code,
			'redirect_uri' => $uri
		);
		$pdata = http_build_query($pdata);

		$params = array('http' => array(
			'method' => 'POST',
			'content' => $pdata
		));

		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		$response = @stream_get_contents($fp);
		
		return $response;
	}
	
	function setWB($url,$access_token,$status){
		$pdata = array ('access_token' => $access_token,
			'status' => $status
		);
		$pdata = http_build_query($pdata);

		$params = array('http' => array(
			'method' => 'POST',
			'content' => $pdata
		));

		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		$response = @stream_get_contents($fp);
		
		return $response;
	}
	
	function setTWB($url,$access_token,$content,$oauth_consumer_key,$openid){
		$pdata = array ('access_token' => $access_token,
			'content' => $content,
			'oauth_consumer_key' => $oauth_consumer_key,
			'openid' => $openid
		);
		$pdata = http_build_query($pdata);

		$params = array('http' => array(
			'method' => 'POST',
			'content' => $pdata
		));

		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		$response = @stream_get_contents($fp);
		
		return $response;
	}
}



/* End of file wap.php */
/* Location: ./application/controllers/web.php */